import Common from './common';

export default class {
    static extends = [Common]

    compCount = {}

    insertNode = (config, selectContainer, nodeItem) => {
        const selectLayoutCell = this.models.page.get('selectLayoutCell');

        config.forEach((item) => {
            if (Array.isArray(item)) {
                this.insertNode(item, selectContainer, nodeItem);
            }
            else if (item && item.id === selectContainer.id) {
                if (item.children === undefined) {
                    item.children = [];
                }

                if (item.areas === 'table' && selectLayoutCell) {
                    let [x, y] = selectLayoutCell.split('-');
                    
                    for(let i=0;i<=x;i++) {
                        if (item.children[i] === undefined) {
                            item.children[i] = [];
                        }

                        for(let j=0;j<=y;j++) {
                            if (item.children[i][j] === undefined) {
                                item.children[i][j] = null;
                            }
                        }
                    }
                    if (!item.children[x][y]) {
                        item.children[x][y] = []
                    }
                    item.children[x][y].push(nodeItem);
                } 
                else {
                    item.children.push(nodeItem);
                }
            } else if (item && Array.isArray(item.children)) {
                this.insertNode(item.children, selectContainer, nodeItem);
            }
        });
    }

    clickItem = (module, item) => {
        return () => {
            const [componentsMap, selectContainer, config] = this.models.page.get(['componentsMap', 'selectContainer', 'config']);
            const componentConfig = componentsMap[module.path][item.path]
            const CustomComponent = componentConfig.component;
            const path = module.path + '/' + item.path;

            if (selectContainer && selectContainer.type !== 'Container' && selectContainer.type !== 'Layout') {
                return;
            }

            // 这块生成id的逻辑有问题
            if (!this.compCount[path]) {
                this.compCount[path] = 1;
            } else {
                this.compCount[path] = this.compCount[path] + 1;
            }

            const nodeConfig = {
                ...item,
                type: module.path,
                component: CustomComponent,
                id: path + this.compCount[path] + Date.now(),
                position: {
                    left: 0,
                    top: 0
                },
                parent: selectContainer,
                areas: componentConfig.areas
            };

            if (selectContainer === null) {
                config.push(nodeConfig);
            } else {
                this.insertNode(config, selectContainer, nodeConfig);
            }

            this.models.page.set({
                config: [...config]
            });
        }
    }
}